/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bbd.crawler.media;

import bbd.crawler.media.fwk.BaseURLMediaHandler;
import bbd.crawler.util.Console;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashSet;
import java.util.Set;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 *
 * @author Harit
 */
public class GoogleSearchHandler extends BaseURLMediaHandler {

    private static final String SPACE_REGEX = " ";
    private static final String REPLACE_WITH_SEPARATOR = "%20";
    private static Set<String> URLs = new HashSet<String>();;
    private static String queryURL = null;
    private static String queryString = null;
    private static int searchPageNumber = 0;
    //--------------------------------------------------------------------------
    /** Javadoc here */
    public GoogleSearchHandler () {

    } // end default constructor

    //--------------------------------------------------------------------------
    /** Javadoc here */
    public void query ( String url, String query ) {

        queryString = query.replaceAll( SPACE_REGEX, REPLACE_WITH_SEPARATOR );
        
        queryURL = url.replaceAll( "SEARCH_STRING", queryString );

        queryURL = queryURL.replaceAll( "START_PAGE", Integer.toString( searchPageNumber ) );

        /* for debugging purposes */
        //System.out.println("URL : " + queryURL + " QueryString : " + queryString);

        processGoogleResults ( executeQuery (queryURL, queryString ) );

    } // end function process

    //--------------------------------------------------------------------------
    /** Javadoc here */
    public static String executeQuery ( String urlString, String queryString) {

        String readLine = null;

        StringBuffer responseData = new StringBuffer();

        try {

            URL url = new URL ( urlString );

            URLConnection connection = url.openConnection();

            BufferedReader reader = new BufferedReader ( new InputStreamReader (connection.getInputStream() ) );

            while ( ( readLine = reader.readLine() ) != null ) {

                responseData.append( readLine );

            }

            return responseData.toString();

        } catch ( Exception e ) {

            Console.error( "GoogleSearchHandler.query",  "Java Exception - " + e.getMessage() );

            return null;

        }

    } // end function executeQuery

    //--------------------------------------------------------------------------
    /** Javadoc here */
    public static void processGoogleResults ( String data ){

        try {

            if ( data == null ) return;
            
            JSONObject jsonObject = new JSONObject ( data );

            /* For debugging pirposes */
            //System.out.println("Total results = " + jsonObject.getJSONObject("responseData").getJSONObject("cursor").getString("estimatedResultCount"));

            JSONArray jsonArray = jsonObject.getJSONObject( "responseData" ).getJSONArray( "results" );

            for ( int i = 0; i < jsonArray.length(); i ++ ) {

                JSONObject entry = jsonArray.getJSONObject( i );

                URLs.add( entry.getString( "url" ) );

            }

            while ( searchPageNumber < 16 ) {

                searchPageNumber += 8;

                queryURL = queryURL.replaceAll("start=\\d", "start="+searchPageNumber);

                processGoogleResults ( executeQuery ( queryURL, queryString ) );

            }

        } catch ( Exception e ) {

            Console.error( "GoogleSearchHandler.process", "Java Exception - " + e.getMessage() );

        }

    } // end function processGoogleResults

    //--------------------------------------------------------------------------
    /* Javadoc here */
    public Object getData() {

        return ( Object ) URLs;

    } // end function getData

} //end class GoogleSearchHandler
